import os
from dataclasses import dataclass
from enum import Enum
from typing import Optional


class EmbeddingProvider(Enum):
    """
    嵌入模型提供商枚举
    """

    HUGGINGFACE = "huggingface"


@dataclass
class EmbeddingConfig:
    """
    嵌入模型配置类
    """

    provider: EmbeddingProvider
    model_name: str
    api_key: Optional[str] = None
    base_url: Optional[str] = None
    timeout: int = 30
    max_retries: int = 3

    def __post_init__(self):
        """
        初始化后验证配置
        """
        # 从环境变量读取配置（如果未提供）
        if self.provider is None:
            provider_str = os.getenv("EMBEDDING_PROVIDER", "huggingface")
            self.provider = EmbeddingProvider(provider_str)

        if self.model_name is None:
            self.model_name = os.getenv("EMBEDDING_MODEL", "./bge-m3/")

        # 验证必填参数
        if not self.provider or not self.model_name:
            raise ValueError("provider和model_name是必填参数")

        # 设置默认超时和重试次数
        if self.timeout <= 0:
            self.timeout = 30

        if self.max_retries < 0:
            self.max_retries = 3
